<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="任务号" prop="jobNo">
        <el-input v-model="queryParams.jobNo" placeholder="请输入任务号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="开始时间" prop="jobStart">
        <el-date-picker clearable v-model="queryParams.jobStart" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择任务开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="jobEnd">
        <el-date-picker clearable v-model="queryParams.jobEnd" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择任务结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="创建人" prop="createName">
        <el-input v-model="queryParams.createName" placeholder="请输入创建人" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="detectList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="任务号" align="center" prop="jobNo" />
      <el-table-column label="任务状态" align="center" prop="jobStatus" />
      <el-table-column label="任务开始时间" align="center" prop="jobStart" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.jobStart, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="任务结束时间" align="center" prop="jobEnd" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.jobEnd, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createName" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['ipmanagement:detect:edit']">查看</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['ipmanagement:detect:remove']">停止</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改探活对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="任务号" prop="jobNo">
          <el-input v-model="form.jobNo" placeholder="请输入任务号" />
        </el-form-item>
        <el-form-item label="任务开始时间" prop="jobStart">
          <el-date-picker clearable v-model="form.jobStart" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择任务开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="任务结束时间" prop="jobEnd">
          <el-date-picker clearable v-model="form.jobEnd" type="date" value-format="yyyy-MM-dd" placeholder="请选择任务结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="创建人" prop="createName">
          <el-input v-model="form.createName" placeholder="请输入创建人" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="实占ip" prop="remark">
          <el-link type="success" @click="shizhanip">{{ shizhanipcount }}</el-link>
        </el-form-item>
        <el-form-item label="空闲ip" prop="remark">
          <el-link type="info" @click="kongxianip">{{ kongxianipcount }}</el-link>
        </el-form-item>
        <el-divider content-position="center">探活子表信息</el-divider>
        <el-table :data="tDetectJobSegmentList" :row-class-name="rowTDetectJobSegmentIndex"
          @selection-change="handleTDetectJobSegmentSelectionChange" ref="tDetectJobSegment">
          <el-table-column label="序号" align="center" prop="index" width="50" />
          <el-table-column label="起始地址" prop="ipStart" width="150">
            <template slot-scope="scope">
              {{scope.row.ipStart}}
            </template>
          </el-table-column>
          <el-table-column label="终止地址" prop="ipEnd" width="150">
            <template slot-scope="scope">
              {{scope.row.ipEnd}}
            </template>
          </el-table-column>
          <el-table-column label="网络分类" prop="ipType" width="150">
            <template slot-scope="scope">
              <el-select v-model="scope.row.ipType" placeholder="请选择网络分类">
                <el-option v-for="dict in dict.type.ip_type" :key="dict.value" :label="dict.label" :value="dict.value"
                  :disabled="true"></el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="用途分类" prop="ipUseType" width="150">
            <template slot-scope="scope">
              <el-select v-model="scope.row.ipUseType" placeholder="请选择用途分类">
                <el-option v-for="dict in dict.type.ip_use_type" :key="dict.value" :label="dict.label"
                  :value="dict.value"></el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="使用地市" prop="ipCity" width="150">
            <template slot-scope="scope">
              <el-select v-model="scope.row.ipCity" placeholder="请选择地市">
                <el-option v-for="dict in dict.type.ip_city" :key="dict.value" :label="dict.label" :value="dict.value">
                </el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="IP地址责任人" prop="ipUser" width="150">
            <template slot-scope="scope">
              {{scope.row.ipUser}}
            </template>
          </el-table-column>
          <el-table-column label="管理部门" prop="ipManagement" width="150">
            <template slot-scope="scope">
              {{scope.row.ipManagement}}
            </template>
          </el-table-column>
          <el-table-column label="设备名称" prop="ipEquipment" width="150">
            <template slot-scope="scope">
              {{scope.row.ipEquipment}}
            </template>
          </el-table-column>
          <el-table-column label="网关IP" prop="ipGateway" width="150">
            <template slot-scope="scope">
              {{scope.row.ipGateway}}
            </template>
          </el-table-column>
          <el-table-column label="使用状态" prop="ipStatus" width="150">
            <template slot-scope="scope">
              <el-select v-model="scope.row.ipStatus" placeholder="请选择使用状态">
                <el-option v-for="dict in dict.type.ip_status" :key="dict.value" :label="dict.label"
                  :value="dict.value"></el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="申请人" prop="ipProposer" width="150">
            <template slot-scope="scope">
              {{scope.row.ipProposer}}
            </template>
          </el-table-column>
          <el-table-column label="申请单" prop="ipApplication" width="150">
            <template slot-scope="scope">
              {{scope.row.ipApplication}}
            </template>
          </el-table-column>
          <el-table-column label="备注" prop="remark" width="150">
            <template slot-scope="scope">
              {{scope.row.remark}}
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">确定</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="title" :visible.sync="ipopen" width="600px" append-to-body>
      <el-table :data="ipstatuslist">
        <el-table-column label="序号" align="center" prop="index" width="50" />
        <el-table-column label="ip地址" prop="ip" width="150" />
        <el-table-column label="ip状态" prop="ipstatus" width="150" />
        <el-table-column label="检测时间" prop="iptime" width="150" />
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="ipcancel">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listKongxianDetect, listShizhanDetect, listDetect, getDetect, delDetect, addDetect, updateDetect } from "@/api/ipmanagement/detect";
export default {
  name: "Detect",
  dicts: ["ip_type", "ip_status", "ip_use_type", "ip_city", "ip_record"],
  data() {
    return {
      ipstatuslist: [],
      shizhanipcount: 0,
      kongxianipcount: 0,
      ipopen: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedTDetectJobSegment: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 探活表格数据
      detectList: [],
      // 探活子表表格数据
      tDetectJobSegmentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        jobNo: null,
        jobStatus: null,
        jobStart: null,
        jobEnd: null,
        createName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        jobNo: [
          { required: true, message: "任务号不能为空", trigger: "blur" }
        ],
        jobStatus: [
          { required: true, message: "任务状态不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    ipcancel() {
      this.ipopen = false;
    },
    shizhanip() {
      this.ipopen = true;
      this.title = "查看实占ip";
      listShizhanDetect(this.form.id, this.queryParams).then(response => {
        debugger
        for (let item of response.rows) {
          item.iptime = this.form.createTime;
          item.ip = item.ipAddress;
          item.ipstatus = "实占";
          this.ipstatuslist.push(item)
        }
      })

    },
    kongxianip() {
      this.ipopen = true;
      this.title = "查看空闲ip";
      listKongxianDetect(this.form.id, this.queryParams).then(response => {
        for (let item of response.rows) {
          item.iptime = this.form.createTime;
          item.ip = item.ipAddress;
          item.ipstatus = "空闲";
          this.ipstatuslist.push(item)
        }
      })
    },
    /** 查询探活列表 */
    getList() {
      this.loading = true;
      listDetect(this.queryParams).then(response => {
        this.detectList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        jobNo: null,
        jobStatus: "0",
        jobStart: null,
        jobEnd: null,
        createName: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.tDetectJobSegmentList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加探活";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDetect(id).then(response => {
        this.form = response.data;
        this.kongxianipcount = response.data.kongxianipcount;
        this.shizhanipcount = response.data.shizhanipcount;
        this.tDetectJobSegmentList = response.data.tdetectJobSegmentList;
        this.open = true;
        this.title = "查看探活";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.tDetectJobSegmentList = this.tDetectJobSegmentList;
          if (this.form.id != null) {
            updateDetect(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDetect(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认停止探活编号为"' + ids + '"的数据项？').then(function () {
        return delDetect(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("停止成功");
      }).catch(() => { });
    },
    /** 探活子表序号 */
    rowTDetectJobSegmentIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 探活子表添加按钮操作 */
    handleAddTDetectJobSegment() {
      let obj = {};
      obj.ipStart = "";
      obj.ipStartI = "";
      obj.ipEnd = "";
      obj.ipEndI = "";
      obj.ipType = "";
      obj.ipUseType = "";
      obj.ipCity = "";
      obj.ipUser = "";
      obj.ipManagement = "";
      obj.ipEquipment = "";
      obj.ipGateway = "";
      obj.ipStatus = "";
      obj.ipProposer = "";
      obj.ipApplication = "";
      obj.recordStatus = "";
      obj.recordType = "";
      obj.remark = "";
      this.tDetectJobSegmentList.push(obj);
    },
    /** 探活子表删除按钮操作 */
    handleDeleteTDetectJobSegment() {
      if (this.checkedTDetectJobSegment.length == 0) {
        this.$modal.msgError("请先选择要删除的探活子表数据");
      } else {
        const tDetectJobSegmentList = this.tDetectJobSegmentList;
        const checkedTDetectJobSegment = this.checkedTDetectJobSegment;
        this.tDetectJobSegmentList = tDetectJobSegmentList.filter(function (item) {
          return checkedTDetectJobSegment.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleTDetectJobSegmentSelectionChange(selection) {
      this.checkedTDetectJobSegment = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ipmanagement/detect/export', {
        ...this.queryParams
      }, `detect_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
